<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'base.item.class.php');

class MailHistoryBase extends BaseItemBase {
	
	//constructor
	function MailHistoryBase() {
		parent::BaseItemBase();
		
		$this->table = 'pp_mails_history';
		$this->prefix = 'mails_';
		
		$this->filter_fields = array('expiry', 'fromuid', 'touid');
		$this->sort_fields = array('id+');
	}
	
	//methods
	//
	function getItemFilter($item) {
		$email_text = $item['mails_content'];
		$email_text = str_replace('\r\n', "\r\n", $email_text);
		$email_text = str_replace('\r', "\r", $email_text);
		$email_text = str_replace('\n', "\n", $email_text);
		$email_text = stripslashes($email_text);
		if( strstr($email_text, '<html>') === false ) {
			$email_text = nl2br($email_text);
		} else {
			$email_text = stripslashes($email_text);
		}
		$item['mails_content'] = $email_text;
		
		return $item;
	}	
	
	//send mail and then log history
	//add mail history
	function bindMailer($mailer) {
		if( is_object($mailer) ) {
			$data_array = array(
					'mails_subject' => $mailer->Subject,
					'mails_content' => $mailer->Body,
					'mails_fromname' => $mailer->From,
					'mails_fromemail' => $mailer->Fromname,
						);
			
			$this->addItem($data_array);
		}
	}
	
	//Data Format
	//	'from_uid'
	//	'from_name'
	//	'from_email_address'
	//	'to_uid'
	//	'to_name'
	//	'to_email_address'
	//	'email_subject'
	//	'email_text'
	function addMH($item) {
		$item = func_db_input($item);
		$data_array = array(
				'mails_fromuid' => $item['from_uid'],
				'mails_fromname' => $item['from_name'],
				'mails_fromemail' => $item['from_email_address'],
				'mails_touid' => $item['to_uid'],
				'mails_toname' => $item['to_name'],
				'mails_toemail' => $item['to_email_address'],
				'mails_subject' => $item['email_subject'],
				'mails_content' => $item['email_text'],
					);
		
		$this->addItem($data_array);
	}
}
//
?>